package com.manulife.hk.performance.config;

import com.google.common.base.Predicate;
import com.google.common.base.Predicates;

import java.util.List;

import static com.google.common.collect.Lists.newArrayList;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.OAuthBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.ClientCredentialsGrant;
import springfox.documentation.service.Contact;
import springfox.documentation.service.GrantType;
import springfox.documentation.service.SecurityScheme;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

@javax.annotation.Generated(value = "io.swagger.codegen.languages.SpringCodegen", date = "2018-03-16T12:42:30.997+08:00")

@Configuration
public class SwaggerDocumentationConfig {

	private static final String SECURITY_SCHEMA_OAUTH2 = "oauth2";
	
    @Value("${swagger.ui.oauth2.token.url}")
    private String swaggerTokenURL;
    
    ApiInfo apiInfo() {
        return new ApiInfoBuilder()
            .title("Performance API")
            .description("This is Performance API")
            .license("Apache 2.0")
            .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")
            .termsOfServiceUrl("")
            .version("1.0.0")
            .contact(new Contact("","", "you@manulife.com"))
            .build();
    }

    @Bean
    public Docket customImplementation(){
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                    .apis(RequestHandlerSelectors.any())
                    .paths(paths())
                    .build()
                    .securitySchemes(newArrayList(oauth()))
                .directModelSubstitute(org.threeten.bp.LocalDate.class, java.sql.Date.class)
                .directModelSubstitute(org.threeten.bp.OffsetDateTime.class, java.util.Date.class)
                .apiInfo(apiInfo());
    }

    @Bean
    public SecurityScheme oauth() {
        return new OAuthBuilder()
                .name(SECURITY_SCHEMA_OAUTH2)
                .grantTypes(grantTypes())
                .scopes(scopes())
                .build();
    }

    private Predicate<String> paths() {
        return Predicates.or(
                PathSelectors.regex("/IssueSales.*"),
                PathSelectors.regex("/SubmitSales.*"),
                PathSelectors.regex("/performance.*"),
                PathSelectors.regex("/SalesMap.*")
        );
    }
    
    private List<AuthorizationScope> scopes() {
        return newArrayList(
                new AuthorizationScope("HK-performance-data.read", "HK-performance-data.read"),
                new AuthorizationScope("HK-performance-data.write", "HK-performance-data.write"));
    }

    private List<GrantType> grantTypes() {  	
    	GrantType grantType = new ClientCredentialsGrant(swaggerTokenURL);     
        return newArrayList(grantType);
    }    

}
